@CLASS
hlpFormater

@USE
pf/types/pfString.p
pf/types/pfValidate.p
pf/types/pfDtf.p

@create[]
# Форматы для дат и чисел
  $_fmt[
     $.date[
       $.short[%e %h]
       $.full[%e %h %Y г.]
  		 $.month[%h]
  		 $.month_year[%h %Y г.]

  		 $.dtfILocale[$pfDtf:ri-low-locale]
  		 $.dtfLocale[$pfDtf:rr-low-locale] 
  		 $.dtfRSLocale[$pfDtf:rs-locale] 
  		 $.weekday[%A]
  	 ]
  	 $.time[
  	   $.normal[%H:%M]
  	 ]
     $.datetime[
       $.normal[%e %h %Y г. %H:%M]
       $.full[%H ч %M мин %e %h %Y г.]
     ]
     $.trafficMB[%.3f]
     $.trafficGB[%.0f]
     $.trafficBytes[%.0f]
     $.money[%.3f]
     $.rur[%.2f]
     $.numeric[%.0f]
  ]

  $_redmineIssues[https://redmine.altair.proc.ru/issues/]

@float[aValue;aPrecision]
## aPrecision - Длина.Точность
  $result[^eval($aValue)[%^if(def $aPrecision){$aPrecision}{.2}f]]
  $result[^result.match[\.][][,]]

@numeric[aValue]
  $result[^pfString:numberFormat[^eval($aValue)[$_fmt.numeric]]]
  $result[^result.match[\-][][–]]

@money[aValue]
  $result[^pfString:numberFormat[^eval($aValue)[$_fmt.rur]]]
  $result[^result.match[\-][][–]]

@rur[aValue]
  $result[^pfString:numberFormat[^eval($aValue)[$_fmt.rur]] руб]
  $result[^result.match[\-][][–]]

@usd[aValue]
  $result[^pfString:numberFormat[^eval($aValue)[$_fmt.money]] у.е.]
  $result[^result.match[\-][][–]]

@month[aDate][result]
  $result[^pfDtf:format[$_fmt.date.month;$aDate;$_fmt.date.dtfILocale]]

@month_year[aDate;isFirstUpper][result]
  $result[^pfDtf:format[$_fmt.date.month_year;$aDate;$_fmt.date.dtfILocale]]
  ^if($isFirstUpper){
    $result[^result.match[^^(.)][]{^match.1.upper[]}]
  }
     
@month_year_r[aDate;isFirstUpper][result]
  $result[^pfDtf:format[$_fmt.date.month_year;$aDate;$_fmt.date.dtfLocale]]
  ^if($isFirstUpper){
    $result[^result.match[^^(.)][]{^match.1.upper[]}]
  }
       
@date[aDate][result]
  $result[^pfDtf:format[$_fmt.date.full;$aDate;$_fmt.date.dtfLocale]]

@time[aDate][result]
  $result[^pfDtf:format[$_fmt.time.normal;$aDate;$_fmt.date.dtfLocale]]

@fullDateAndTime[aDate][result]
  $result[^pfDtf:format[$_fmt.datetime.full;$aDate;$_fmt.date.dtfLocale]]

@dateAndTime[aDate][result]
  $result[^pfDtf:format[$_fmt.datetime.normal;$aDate;$_fmt.date.dtfLocale]]

@weekday[aDate][result]
  $result[^pfDtf:format[$_fmt.date.weekday;$aDate;$_fmt.date.dtfILocale]]

@r_weekday[aDate][result]
  $result[^pfDtf:format[$_fmt.date.weekday;$aDate;$_fmt.date.dtfLocale]]

@short_weekday[aDate][result]
  $result[^pfDtf:format[$_fmt.date.weekday;$aDate;$_fmt.date.dtfRSLocale]]
  $result[^result.lower[]]

@phone[aString]
## Форматирует номера телефонов в строке в нормальный вид
  $result[^aString.match[(?:(\d+?))?(?:(\d{3}))?(\d{3})(\d{2})(\d{2})([\p{Z}\p{P}]|^$)][g]{^if(def $match.1){${match.1}-}^if(def $match.2){${match.2}-}${match.3}-${match.4}-${match.5}$match.6}]
#$result[$aString]

@numberDecline[num;nominative;genitive_singular;genitive_plural]
 $result[^pfString:numberDecline[$num;$nominative;$genitive_singular;$genitive_plural]]
 
@comment[aComment;aOptions]       
  $aOptions[^hash::create[$aOptions]]
  $result[^aComment.match[((?:[A-Z\-0-9]+?\:(?://)?)(?:\S+?(?:\:\S+)?@)?[A-Z0-9\-\.]+\.[A-Z]{2,5}(?:\:\d+)?(?:(?:/|\?)\S*)?)][gi]{<a href="$match.1" target="_blank">$match.1</a>}]
  $result[^result.match[($pfValidate:validEmailRegex)][gi]{<a href="mailto:$match.1">$match.1</a>}]
  $result[^result.match[\n][g]{<br />}]

@parseSumma[aSumma]
## Парсит сумму
  $result[^aSumma.match[^^\s*\-?\s*(\d+)\s*(?:(?:[\.\,]\s*)(\d+))?.*^$][]{${match.1}^if(def $match.2){.${match.2}}}]
  $result[^eval(^result.double(0))[%.2f]]
